

package com.maoshi.shop.coupon.platform.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maoshi.shop.bean.enums.OfflineHandleEventType;
import com.maoshi.shop.bean.model.OfflineHandleEvent;
import com.maoshi.shop.bean.param.OfflineHandleEventAuditParam;
import com.maoshi.shop.common.config.Constant;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.coupon.comment.model.Coupon;
import com.maoshi.shop.coupon.comment.service.CouponService;
import com.maoshi.shop.security.platform.util.SecurityUtils;
import com.maoshi.shop.service.OfflineHandleEventService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController("adminCouponController")
@RequestMapping("/platform/coupon")
@AllArgsConstructor
public class CouponController {

    private final CouponService couponService;
    private final OfflineHandleEventService offlineHandleEventService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('platform:coupon:page')")
    public ResponseEntity<IPage<Coupon>> page(Coupon coupon, PageParam<Coupon> page) {
        IPage<Coupon> couponPage = couponService.getPlatformPage(page, coupon);
        return ResponseEntity.ok(couponPage);
    }

    /**
     * 获取平台下优惠券
     * @return
     */
    @GetMapping("/list")
    public IPage<Coupon> couponList(PageParam<Coupon> page){
        IPage<Coupon> couponList = couponService.page(page, new LambdaQueryWrapper<Coupon>().eq(Coupon::getShopId, Constant.PLATFORM_SHOP_ID));
        return couponList;
    }

    /**
     * 分页获取平台优惠券
     */
    @GetMapping("/pageByPlatform")
    @PreAuthorize("@pms.hasPermission('platform:coupon:page')")
    public ResponseEntity<IPage<Coupon>> pageByPlatform(Coupon coupon, PageParam<Coupon> page) {
        coupon.setShopId(Constant.PLATFORM_SHOP_ID);
        IPage<Coupon> couponPage = couponService.getPlatformPage(page, coupon);
        return ResponseEntity.ok(couponPage);
    }
    /**
     * 修改优惠券不为积分商品
     */
    @PutMapping("/removeScore")
    @PreAuthorize("@pms.hasPermission('platform:coupon:delete')")
    public ResponseEntity<IPage<Coupon>> removeScore(@RequestBody Long couponId) {
        Coupon coupon = couponService.getById(couponId);
        coupon.setIsScoreType(0);
        coupon.setScorePrice(0);
        couponService.updateById(coupon);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改平台券可以积分兑换
     */
    @PutMapping("/couponByScore")
    @PreAuthorize("@pms.hasPermission('platform:coupon:update')")
    public ResponseEntity<IPage<Coupon>> couponByScore(@RequestBody @Valid Coupon coupon) {
        if(coupon.getScorePrice() == null){
            throw new MaoshiShopBindException("请输入正确的积分数量");
        }
        coupon.setIsScoreType(1);
        couponService.updateById(coupon);
        return ResponseEntity.ok().build();
    }

    /**
     * 分页获取积分可换优惠券
     */
    @GetMapping("/scorePage")
    @PreAuthorize("@pms.hasPermission('platform:coupon:page')")
    public ResponseEntity<IPage<Coupon>> pageByScore(Coupon coupon, PageParam<Coupon> page) {
        coupon.setIsScoreType(1);
        IPage<Coupon> couponPage = couponService.getPlatformPage(page, coupon);
        return ResponseEntity.ok(couponPage);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('platform:coupon:info')")
    public ResponseEntity<Coupon> info(@PathVariable("id") Long id) {
        Coupon coupon = couponService.getCouponAndCouponProdsByCouponId(id);
        return ResponseEntity.ok(coupon);
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform:coupon:delete')")
    public ResponseEntity<Void> delete(@RequestBody Long couponId) {
        couponService.deleteByCouponId(couponId);
        return ResponseEntity.ok().build();
    }

    /**
     * 通过活动id获取下线信息
     */
    @GetMapping("/getOfflineHandleEventByCouponId/{couponId}")
    @PreAuthorize("@pms.hasPermission('platform:coupon:info')")
    public ResponseEntity<OfflineHandleEvent> getOfflineHandleEventByCouponId(@PathVariable("couponId") Long couponId) {
        OfflineHandleEvent offlineHandleEvent = offlineHandleEventService.getProcessingEventByHandleTypeAndHandleId(OfflineHandleEventType.COUPON.getValue(), couponId);
        return ResponseEntity.ok(offlineHandleEvent);
    }

    /**
     * 下线活动
     */
    @PostMapping("/offline")
    @PreAuthorize("@pms.hasPermission('platform:coupon:update')")
    public ResponseEntity<Void> offline(@RequestBody OfflineHandleEvent offlineHandleEvent) {
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        Coupon coupon = couponService.getCouponAndCouponProdsByCouponId(offlineHandleEvent.getHandleId());
        if (coupon == null) {
            throw new MaoshiShopBindException("未找到该活动信息");
        }
        couponService.offline(coupon, offlineHandleEvent.getOfflineReason(), sysUserId);
        return ResponseEntity.ok().build();
    }

    /**
     * 审核活动
     */
    @PostMapping("/auditCoupon")
    @PreAuthorize("@pms.hasPermission('platform:coupon:update')")
    public ResponseEntity<Void> auditCoupon(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        Coupon coupon = couponService.getCouponAndCouponProdsByCouponId(offlineHandleEventAuditParam.getHandleId());
        if (coupon == null) {
            throw new MaoshiShopBindException("未找到该活动信息");
        }
        couponService.auditCoupon(offlineHandleEventAuditParam, sysUserId);
        // 移除缓存
        couponService.removeCouponAndCouponProdsCache(coupon.getShopId());
        return ResponseEntity.ok().build();
    }


    /**
     * 保存
     */
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('platform:coupon:save')")
    public ResponseEntity<Void> save(@RequestBody @Valid Coupon coupon) {
        // 此处新增优惠券皆为平台通用
        coupon.setShopId(Constant.PLATFORM_SHOP_ID);
        coupon.setSourceStock(coupon.getStocks());
        couponService.insertCouponAndCouponProds(coupon);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
    public ResponseEntity<Void> update(@RequestBody @Valid Coupon coupon) {
        // 查询平台下优惠券
        Coupon dbCoupon = couponService.getById(coupon.getCouponId());
        if(null == dbCoupon){
            throw new MaoshiShopBindException("优惠券不存在");
        }
        coupon.setShopId(dbCoupon.getShopId());
        couponService.updateCouponAndCouponProds(coupon);
        return ResponseEntity.ok().build();
    }


}
